package com.financing.wap.weixin.handle.event;

import java.util.Date;
import java.util.Map;

import me.chanjar.weixin.mp.bean.result.WxMpUser;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.common.CommonConstantUtil;
import com.financing.wap.dto.DstUserInfoDTO;
import com.financing.wap.service.IDstUserInfoService;
import com.financing.wap.service.weixin.service.WeixinService;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.EmojiFilterUtil;
import com.financing.wap.weixin.handle.CreatePosterHandler;
import com.financing.wap.weixin.handle.EventHandler;

/**
* 扫描带参数二维码事件-用户已关注时的事件推送
* @author lisu
*
*/
@Service("locationEvent")
public class LocationEvent implements EventHandler{
	
	Logger logger = Logger.getLogger(getClass());
	
	
	public static final  Object b = new Object();

	@Autowired
	private IDstUserInfoService dstUserInfoService;
	
	
	@Autowired
	private CreatePosterHandler createPosterHandler;
	
    @Autowired
    private WeixinService weixinJincsServiceImpl;
	
	@Override
	public String messageHandler(Map<String, String> requestMap) throws Exception {
		
        String fromUserName = requestMap.get("FromUserName");  // 发送方帐号（open_id） 
        String toUserName = requestMap.get("ToUserName");// 公众帐号  
        String latitude = requestMap.get("Latitude");  // 地理位置纬度
        String longitude = requestMap.get("Longitude");// 地理位置经度
        synchronized (b) {
	        DstUserInfoDTO querBean = new DstUserInfoDTO();
	        querBean.setUserWechatOpenid(fromUserName);
	        
	        DstUserInfoDTO userInfo = dstUserInfoService.queryListGetFirst(querBean);
	        
	        if(userInfo != null){
	        	userInfo.setUserWechatX(latitude);
	        	userInfo.setUserWechatY(longitude);
	        	dstUserInfoService.update(userInfo);
	        }else{
				WxMpUser mpUser = weixinJincsServiceImpl.getUserInfoForUserService(fromUserName);
				userInfo=new DstUserInfoDTO();
				userInfo.setUserWechatOpenid(mpUser.getOpenId());
				userInfo.setUserName(EmojiFilterUtil.filterEmoji(mpUser.getNickname()));
				userInfo.setUserWechatImage(mpUser.getHeadImgUrl());
				userInfo.setUserWechatAddress(mpUser.getProvince()+mpUser.getCity());
				userInfo.setFlagConcern(CommonConstantUtil.UserInfoStatus.FLAG_CONCERN_IS);// 已关注
				userInfo.setDeleteTag(CommonConstantUtil.CommonStatus.DELETE_TAG_NORMAL);// 否删除
				userInfo.setCreateDate(new Date(System.currentTimeMillis()));
				userInfo.setUpdateDate(new Date(System.currentTimeMillis()));
				userInfo.setRegisterDate(DateUtil.getString(
						userInfo.getCreateDate(), "yyyy-MM-dd"));
				userInfo.setCreateBy("system");
				userInfo.setUpdateBy("system");
				// 新增用户
				dstUserInfoService.insert(userInfo);
				requestMap.put("userId", userInfo.getId().toString());
	        }
        }
		return "success";
	}

	@Override
	public void messageSubsequentHandler(Map<String, String> requestMap) throws Exception {
		
		
		
	}

}

